# -*- coding: UTF-8 -*-
"""
@Project ：swift-forge-engine
@File    ：errors.py
@IDE     ：PyCharm 
@Author  ：Tony.Gan
@Date    ：2025/1/18 23:44 
@Des     ：
"""

from typing import Any

from fastapi import HTTPException
from starlette.background import BackgroundTask

from App.common.respones.response_code import StatusResponseCode, CustomResponseCode


class BaseExceptionMixin(Exception):

    def __init__(self, *, code: int, message: str = None, data: Any = None, background: BackgroundTask | None = None):
        self.code = code
        self.message = message
        self.data = data
        self.background = background


class HTTPError(HTTPException):
    """
    HTTP请求异常
    """

    def __init__(self, *, code: int, message: Any = None, headers: dict[str, Any] | None = None):
        super().__init__(status_code=code, detail=message, headers=headers)


class ServerError(BaseExceptionMixin):
    """
    服务器500异常
    """
    code = StatusResponseCode.HTTP_500

    def __init__(
            self, *, message: str = 'Internal Server Error', data: Any = None, background: BackgroundTask | None = None
    ):
        super().__init__(code=self.code, message=message, data=data, background=background)


class GatewayError(BaseExceptionMixin):
    """
    网关异常
    """
    code = StatusResponseCode.HTTP_502

    def __init__(self, *, message: str = 'Bad Gateway', data: Any = None, background: BackgroundTask | None = None):
        super().__init__(code=self.code, message=message, data=data, background=background)


class RequestError(BaseExceptionMixin):
    """错误请求异常"""
    code = StatusResponseCode.HTTP_400

    def __init__(self, *, message: str = 'Bad Request', data: Any = None, background: BackgroundTask | None = None):
        super().__init__(code=self.code, message=message, data=data, background=background)


class AuthorizationError(BaseExceptionMixin):
    """未登录或TOKEN过期"""
    code = StatusResponseCode.HTTP_401

    def __init__(self, *, message: str = 'Permission Denied', data: Any = None, background: BackgroundTask | None = None):
        super().__init__(code=self.code, message=message, data=data, background=background)


class ForbiddenError(BaseExceptionMixin):
    """403权限不足异常"""
    code = StatusResponseCode.HTTP_403

    def __init__(self, *, message: str = 'Forbidden', data: Any = None, background: BackgroundTask | None = None):
        super().__init__(code=self.code, message=message, data=data, background=background)


class NotFoundError(BaseExceptionMixin):
    """404未找到资源异常"""
    code = StatusResponseCode.HTTP_404

    def __init__(self, *, message: str = 'Not Found', data: Any = None, background: BackgroundTask | None = None):
        super().__init__(code=self.code, message=message, data=data, background=background)


class CustomError(BaseExceptionMixin):
    """用户自定义异常"""
    def __init__(self, *, message: str = "", data: Any = None, background: BackgroundTask | None = None):
        self.code = CustomResponseCode.USER_ERROR.code
        if message:
            self.message = CustomResponseCode.USER_ERROR.message
        super().__init__(code=self.code, message=message, data=data, background=background)













